define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'examine/show_examine/index' + location.search,
                    add_url: 'examine/show_examine/add',
                    edit_url: 'examine/show_examine/edit',
                    del_url: 'examine/show_examine/del',
                    multi_url: 'examine/show_examine/multi',
                    import_url: 'examine/show_examine/import',
                    table: 'show_examine',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('用户昵称'), operate: 'LIKE'},
                        {
                            field: '_before', title: __('原数据'),
                            align: 'left',
                            formatter: function (value, index) {
                                if (value) {
                                    var array = [];
                                    var groupNameArray = value.split('|');
                                    for (var i = 0; i < groupNameArray.length; i++) {
                                        array.push(groupNameArray[i]);
                                    }
                                    return array.join('<br>');
                                }
                                return value;
                            }
                        },
                        {
                            field: '_after', title: __('修改后数据'),
                            align: 'left',
                            formatter: function (value, index) {
                                if (value) {
                                    var array = [];
                                    var groupNameArray = value.split('|');
                                    for (var i = 0; i < groupNameArray.length; i++) {
                                        array.push(groupNameArray[i]);
                                    }
                                    return array.join('<br>');
                                }
                                return value;
                            }
                        },
                        {
                            field: 'type', title: __('类型'),
                            searchList: {1: __('个人主页'), 2: __('课程'), 3: __('师资')},
                            formatter: Table.api.formatter.label
                        },
                        {
                            field: 'status', title: __('审核状态'),
                            searchList: {1: __('待审核'), 2: __('审核成功'), 3: __('审核不通过')},
                            formatter: Table.api.formatter.label
                        },
                        {field: 'note', title: __('Note'), operate: 'LIKE', table: table, class: 'autocontent'},
                        {field: 'admin.nickname', title: __('管理员'), operate: 'LIKE'},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 通过
            $('.btn-adopt').click(function () {
                var ids = JSON.stringify(Table.api.selectedids(table));
                $.post('examine/show_examine/adopt', {'ids': ids}, function (data) {
                    if (parseInt(data.code) === 1) {
                        Toastr.success("成功");
                        setTimeout(function () {
                            location.reload(); // 刷新当前页面
                        }, 1500);
                    } else {
                        Toastr.error("请求失败，请稍后重试");
                        setTimeout(function () {
                            location.reload(); // 刷新当前页面
                        }, 1500);
                    }
                    console.log(data);
                }, 'JSON');
            });

            // 拒绝
            $('.btn-refuse').click(function () {
                var ids = JSON.stringify(Table.api.selectedids(table));
                Fast.api.open('examine/show_examine/refuse?ids=' + ids, '一键拒绝', {area:["70%", "70%"]});
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        refuse: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
